package com.ebt.m.homepage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.activity.WebBaseActivity;
import com.ebt.m.data.rxModel.apibean.PerformanceAll;
import com.ebt.m.utils.DateUtils;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.utils.NumberHelper;
import com.ebt.m.utils.TalkingDataUtil;
import com.ebt.m.commons.utils.ToastUtil;
import com.ebt.m.commons.utils.NetUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PerformanceView extends DynamicBaseView {

    @BindView(R.id.performance_title)
    TextView performanceTitle;
    @BindView(R.id.FYP_count)
    TextView FYPCount;
    @BindView(R.id.FYC_count)
    TextView FYCCount;
    @BindView(R.id.newcatractCount)
    TextView newcatractCount;
    @BindView(R.id.checkInCount)
    TextView checkInCount;
    @BindView(R.id.issueCount)
    TextView issueCount;
    @BindView(R.id.content)
    RelativeLayout mContent;
    private PerformanceAll performanceAll;

    public PerformanceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PerformanceView(Context context) {
        this(context, null);
    }

    public PerformanceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.layout_4homepage_performance, this);
        ButterKnife.bind(this, this);
    }

    @Override
    public void update(Object... args) {
        performanceTitle.setText(DateUtils.getCurrentMonth() + "月受理新契约");
        try {
            performanceAll = (PerformanceAll) args[0];
            FYPCount.setText(NumberHelper.formatDouble(performanceAll.getCount().getFyp()));
            FYCCount.setText(NumberHelper.formatDouble(performanceAll.getCount().getFyc()));
            newcatractCount.setText(performanceAll.getCount().getNewContractCount() + "");
            checkInCount.setText(performanceAll.getCount().getCheckInCount() + "");
            issueCount.setText(performanceAll.getCount().getIssueCount() + "");
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    @OnClick(R.id.content)
    public void onContentClick() {

        if (performanceAll == null) {
            ToastUtil.show(getContext(), R.string.network_fail);
        } else {
            if (!NetUtils.isNetworkConnected(getContext())) {
                ToastUtil.show(getContext(), R.string.network_fail);
            } else {
                try {
                    if (TextUtils.isEmpty(performanceAll.getCount().getNewContractLink()))
                        return;
                    TalkingDataUtil.onEventWithAgentId(TalkingDataUtil.TYPE_DASHBOADD_V270);
                    Intent webIntent = new Intent(getContext(), WebBaseActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(WebBaseActivity.WEB_TYPE_TITLE, "新契约");
                    bundle.putBoolean(WebBaseActivity.WEB_TYPE_HIDE_TOOLBAR, true);
                    bundle.putString(WebBaseActivity.WEB_TYPE_LINK, performanceAll.getCount().getNewContractLink());
                    bundle.putInt(WebBaseActivity.WEB_TYPE_FRAGMENT, WebBaseActivity.FRAGMENT_4NEW_BASE);
                    webIntent.putExtras(bundle);
                    getContext().startActivity(webIntent);
                } catch (Exception e) {
                    Logger.e(e);
                }
            }
        }

    }

}
